// pages/aacplace/aacplace.js
var app = getApp()
var aa=1
var openid,userinfo,isRelease
var acidacid,sitesite=0
Page({
  
  data: {
    acid:'',
    state:'',sitesite:'',
    site:'',
    placemsg:''
  },
  zhuye(){
    wx.redirectTo({
      url: '../ayeshou/yeshou',
    })
  },
  onLoad: function (options) {
    
    // if(aa ==1){
      let scene = options.scene;
      var tzid = options.id
      var xuhao = options.xuhao
      console.log(scene,tzid,xuhao)
      // let scene=decodeURIComponent(options.scene);

      var placemsg = wx.getStorageSync('placemsg')
      // if(placemsg){
      //   console.log('跳转进入页面，要获取本地缓存')
      //   acidacid=scene.split(".")[0];
      //   sitesite=scene.split('.')[1];
      //   console.log(acidacid,sitesite)
      // }
      if(scene){
        console.log('aa=1,扫描小程序码进入页面')
        acidacid=scene.split(".")[0];
        sitesite=scene.split('.')[1];
        isRelease=scene.split('.')[2];
        console.log(acidacid,sitesite)
      }else if(tzid){
        console.log('aa=1,扫描二维码进入页面')
        //页面跳转
        acidacid = options.id
        sitesite = options.site
        isRelease = options.state
      // }else if(xuhao){
      //   console.log('aa=1,跳转进入页面')
      //   //页面跳转
      //   // acidacid = options.xuhao
      //   sitesite = options.xuhao
      }
    if(acidacid==undefined){
      acidacid = 256;sitesite=5;isRelease=3
      acidacid = 269;sitesite=4;isRelease=2
      acidacid = 275;sitesite=1;isRelease=3
      console.log('acid设置默认值')
    }
    this.setData({sitesite:sitesite})
    console.log('进入页面-活动id',acidacid,'site',sitesite)
    openid = wx.getStorageSync('openid')
    // var userinfo = app.globalData.userInfo
    userinfo = wx.getStorageSync('headimg')
    
    console.log('授权后获取的信息',userinfo)
    // if(openid==''||openid=='undefined'||avatar==undefined){
      if(openid==''||openid=='undefined'||userinfo==undefined||userinfo==''){
      console.log(openid)
      wx.navigateTo({
        url: '../shouquan/shouquan',
      })
    }

    var tiaozhuan=2
    var acename
      // 扫码进入此页面，有options传递的值,如果有传参，修改tiaozhuan，无法获取本地数据
      // 再次进来没有传参数，if不成立，但是sitesite和acidacid是有的
      // 这个if判断不成立的话，就会取本地缓存,有其中之一就可以设置标题，不取本地文件
      
    if(aa ==1){
      if(options.site!=undefined||options.scene!=undefined){
        tiaozhuan = 3
        if(sitesite==1){
          acename = '朝阳公园'
        }
        if(sitesite==2){
          acename = '天泽路'
        }
        if(sitesite==3){
          acename = '水长城'
        }
        if(sitesite==4){
          acename = '古北水镇'
        }
        if(sitesite==5){
          acename = '云顶酒店'
        }
        wx.setNavigationBarTitle({
          title: acename,
        })
        this.setData({acid:options.id,state:options.state,site:options.site})
      }else{
      // 没有参数，直接打开、或从首页进入此页面，从本地存储获取信息
    //  if(tiaozhuan==2){
      //  wx.setNavigationBarTitle({
      //     title: placemsg.placename,
      //   })
        // 从首页进入此页面，从本地存储获取信息  data.state
        // if(placemsg.placename=='天泽路'){
        //   this.setData({acid:126,state:1,site:2})
        //   acidacid=126
        //   sitesite=2
        // }
        // if(placemsg.placename=='朝阳公园'){
        //   this.setData({acid:259,state:1,site:1})
        //   acidacid=259
        //   sitesite=1
        // }
        // if(placemsg.placename=='水长城'){
        //   this.setData({acid:155,state:1,site:3})
        //   acidacid=264
        //   sitesite=3
        // }
        // if(placemsg.placename=='古北水镇'){
        //   this.setData({acid:269,state:1,site:4})
        //   acidacid=269
        //   sitesite=4
        // }
        // acidacid=221
      }
      aa = 2
    }
  },
  //立即开始
  start(){
    var that = this
    wx.request({
      url: 'https://www.juquduo.com/juquduo/join_activity/is_applied',
      data:{
        // activityId:192,
        activityId:acidacid,
        openid:openid,
      },
      success(res){
        console.log(res.data)
        var code = res.data.code
        if(code=='100'){
          openid = wx.getStorageSync('openid')
          userinfo = wx.getStorageSync('headimg')
          console.log('userinfo',userinfo.nickName)
          if(userinfo==undefined||userinfo==''||userinfo.nickName==undefined){
            wx.redirectTo({
              url: '../shouquan/shouquan',
            })
          }else{
            wx.showModal({
            title:'正在进入单人游戏，确认报名？',
            success(res){
              if (res.confirm) {
                wx.request({
                  url: 'https://www.juquduo.com/juquduo/join_activity/apply',
                  data:{
                    openid:openid,
                    activityId:acidacid,
                    avatar:userinfo.avatarUrl,
                    nick_name:userinfo.nickName,
                  },
                  success(res){
                    console.log(res)//报名成功
                    if(res.data.code=='200'){
                      wx.navigateTo({
                        url: '../dacmode/dacmode?id='+acidacid+'&isRelease='+isRelease+'&site='+sitesite,
                        // url: '../dacmode/dacmode?id='+261+'&state='+2+'&site='+3,
                      })
                      console.log('跳转到dacmode,活动模板,->单人游戏和房建游戏')
                    }else{
                      console.log('申请进入失败',acidacid,openid,res)
                    }
                  },
                  fail(res){
                    console.log('请求失败',res)
                  }
                })}}})}
        }//已经报名
        if(code=='200'){
          wx.navigateTo({
            url: '../dacmode/dacmode?id='+acidacid+'&isRelease='+isRelease+'&site='+sitesite,
            // url: '../dacmode/dacmode?id='+261+'&state='+2+'&site='+3,
          })
          console.log('跳转到dacmode,活动模板')
        }}
    })
  },
  //去分组
  signup(){
    openid = wx.getStorageSync('openid')
    // wx.clearStorageSync()
    userinfo = wx.getStorageSync('headimg')
    console.log('userinfo',userinfo.nickName)
    if(userinfo==undefined||userinfo==''||userinfo.nickName==undefined){
      wx.redirectTo({
        url: '../shouquan/shouquan',
      })
    }else{
      wx.request({
        url: 'https://www.juquduo.com/juquduo/join_activity/apply',
        data:{
          openid:openid,
          activityId:acidacid,
          avatar:userinfo.avatarUrl,
          nick_name:userinfo.nickName,
        },
        success(res){
          console.log(res)
        }
      })
      wx.navigateTo({
        url:'../group2/zjfz?id='+acidacid+'&site='+sitesite+'&isRelease='+isRelease,
      })
      console.log('跳转到团建页面') 
    } 
  },
  onShow(){
    openid = wx.getStorageSync('openid')
    console.log('app.js里的信息',userinfo)
  },
  tomons(){
    wx.redirectTo({
      url: '../animate/index',
    })
  },
  toh5(){
    wx.redirectTo({
      url: '../dgame1dt/index',
    })
  }
})
